package com.qishanor.wpan.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qishanor.framework.common.oss.config.FileProperties;
import com.qishanor.framework.util.R;
import com.qishanor.wpan.model.WpanFile;
import com.qishanor.wpan.service.WpanFileService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Wrapper;
import java.util.List;

/**
 * @description:
 * @author: 周振林
 * @date: 2023-05-24
 * @website: http://www.zhouzhenlin.com - 沉淀、分享、成长、让自己和他人都有所收获
 **/

@RestController
@RequestMapping("/api/wpan")
@AllArgsConstructor
public class WpanFileController {

    private final FileProperties properties;
    private final WpanFileService wpanFileService;

    @GetMapping("/list")
    public R list(Page page,String name, String type){

       IPage results=wpanFileService.page(page,Wrappers.<WpanFile>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(name),WpanFile::getRealName,name)
                .eq(ObjectUtil.isNotEmpty(type),WpanFile::getCategory,type)
                .eq(WpanFile::getIsRecycle,0).orderByDesc(WpanFile::getDirType));


       results.getRecords().forEach(u->{
            WpanFile file=(WpanFile) u;
            file.setUrl("https://"+properties.getBucketName()+"."+properties.getOss().getEndpoint()+"/"+file.getUrl());
       });

        return R.ok(results);
    }


    /**
     * 上传文件 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
     * @param file 资源
     * @return R(/ admin / bucketName / filename)
     */
    @PostMapping(value = "/uploadFile")
    public R uploadFile(@RequestPart("file") MultipartFile file) {


        return  wpanFileService.uploadFile(file);
    }


    @GetMapping("/getRecycle")
    public R getRecycle(){
       List<WpanFile> results= wpanFileService.list(Wrappers.<WpanFile>lambdaQuery().eq(WpanFile::getIsRecycle,1));

       return R.ok(results);
    }

    @PutMapping("/remove")
    public R remove(@RequestBody  Long[] fileIds){
        List<WpanFile> lists=wpanFileService.list(Wrappers.<WpanFile>lambdaQuery().in(WpanFile::getId,fileIds));

        for(WpanFile file:lists){
            file.setIsRecycle(1);
        }
        wpanFileService.updateBatchById(lists);
        return R.ok();
    }

    @PutMapping("/recovery")
    public R recovery(@RequestBody Long[] fileIds){
        List<WpanFile> lists=wpanFileService.list(Wrappers.<WpanFile>lambdaQuery().in(WpanFile::getId,fileIds));

        for(WpanFile file:lists){
            file.setIsRecycle(0);
        }
        wpanFileService.updateBatchById(lists);

        return R.ok();
    }
    @PutMapping("/trash")
    public R trash(@RequestBody Long[] fileIds){


        List<WpanFile> lists=wpanFileService.list(Wrappers.<WpanFile>lambdaQuery().in(WpanFile::getId,fileIds));
        wpanFileService.removeByIds(lists);

        return R.ok();
    }

    /**
     * 清空回收站
     */
    @PutMapping("/clearRecycle")
    public R clearRecycle(){

        List<WpanFile> lists=wpanFileService.list(Wrappers.<WpanFile>lambdaQuery().eq(WpanFile::getIsRecycle,1));
        wpanFileService.removeByIds(lists);

        return R.ok();
    }


}
